package com.huarun.module.other.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.huarun.common.BaseController;
import com.huarun.common.util.MyException;
import com.huarun.common.util.PageBean;
import com.huarun.common.util.ResultCodeValue;
import com.huarun.module.other.bean.OtherApply;
import com.huarun.module.other.service.IOtherApplyService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author hqy
 * @创建时间 2018-10-26 15:23
 * @Description 其他申请
 */
@Api(description = "其他申请")
@Controller
@RequestMapping(value = { "/wx/otherApply", "/pc/otherApply" })
public class OtherApplyController extends BaseController {
	@Resource
	private IOtherApplyService service;

	@ApiOperation(value = "其他申请查询", notes = "其他申请查询")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public Object list(@RequestParam(required = false) Integer page, @RequestParam(required = false) Integer rows,
			HttpServletRequest request) {
		//因为小程序和PC用了通一个接口，但是Header中前端存放的字段不同，所以先从Header中取projectId,取不到再取Authorization
		String projectId = request.getHeader("projectId");
		if (null == projectId) {
			String headAuth = request.getHeader("Authorization");
			JSONObject headObj = JSONObject.parseObject(headAuth);
			projectId = headObj.getString("projectId");
			if(null==projectId) {
				throw new MyException(ResultCodeValue.R_PARAM_REQUIRED, "请选择项目");
			}
		}
		Map<String, Object> inMap = new HashMap<String, Object>();
		inMap.put("projectId", projectId);
		inMap.put("page", page);
		inMap.put("rows", rows);
		PageBean<OtherApply> data = service.findListPage(inMap);
		return getOperResultMap("查询其他申请成功", data);
	}

	@ApiOperation(value = "新增/修改其他申请", notes = "新增/修改其他申请")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public Object save(@RequestBody OtherApply other, HttpServletRequest request) {
		String headAuth = request.getHeader("Authorization");
		JSONObject headObj = JSONObject.parseObject(headAuth);
		String projectId = headObj.getString("projectId");
		if (other == null) {
			throw new MyException(ResultCodeValue.R_PARAM_REQUIRED, "不能为空");
		}
		if (StringUtils.isBlank(other.getApplyName())) {
			throw new MyException(ResultCodeValue.R_PARAM_REQUIRED, "申请名称不能为空");
		}
		if (other.getPhone() == null) {
			throw new MyException(ResultCodeValue.R_PARAM_REQUIRED, "联系电话不能为空");
		}
		if (other.getApplyName().length() > 20 || other.getPhone().length() > 20) {
			throw new MyException(ResultCodeValue.R_PARAM_ERROR, "联系电话或是申请名称长度超过最大字符限制");
		}

		String msg = "修改成功";
		if (other.getId() == null) {
			other.setProjectId(projectId);
			other.setCreateDate(new Date());
			service.save(other);
			msg = "新增成功";
		} else {
			other.setUpdateDate(new Date());
			service.update(other);
		}
		return getOperResultMap(msg);
	}

	@ApiOperation(value = "删除", notes = "删除")
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	@ResponseBody
	public Object delete(@RequestParam(required = false) Integer id) {
		try {
			service.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
			return getOperResultMap("删除失败");
		}
		return getOperResultMap("删除成功");
	}
}
